#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QInputDialog>


#include "window.h"

Window::Window(){
	qrError = 0.01;
	
	glWidget = new GLWidget;
	setCentralWidget(glWidget);
	
	statusbar = new QStatusBar(this);
	setStatusBar(statusbar);
	
	
	
	/////////////////////////////////////////////////File/////////////////////////////////////////////////
	QMenu* menuFile = new QMenu(this);
	menuFile->setTitle(tr("&File"));
	
	QAction* exit = new QAction(this);
	exit->setText(tr("Exit"));
	exit->setStatusTip(tr("Bye bye!"));
	connect(exit,SIGNAL(triggered()),this,SLOT(close()));
	
	menuFile->addAction(exit);
	
	
	/////////////////////////////////////////////////System/////////////////////////////////////////////////
	QMenu* menuSystem = new QMenu(this);
	menuSystem->setTitle(tr("&System"));
	
	//mass spring
	QAction* create_mass_spring = new QAction(this);
	create_mass_spring->setText(tr("Mass-spring system"));
	create_mass_spring->setStatusTip(tr("Create a new mass-spring system."));
	connect(create_mass_spring,SIGNAL(triggered()),this,SLOT(setNumberMasses()));
		
	menuSystem->addAction(create_mass_spring);
	
	//building
	QAction* create_building = new QAction(this);
	create_building->setText(tr("Building system"));
	create_building->setStatusTip(tr("Create a new building system."));
	connect(create_building,SIGNAL(triggered()),this,SLOT(setNumberFloors()));
		
	menuSystem->addAction(create_building);
	
	
	menuSystem->addSeparator();
	
	//graphic
	QAction* show_graphics = new QAction(this);
	show_graphics->setText(tr("Show graphic"));
	show_graphics->setStatusTip(tr("Show graphic of one DOF (d x t)."));
	connect(show_graphics,SIGNAL(triggered()),this,SLOT(showGraphic()));
		
	menuSystem->addAction(show_graphics);
	
	
	/////////////////////////////////////////////////Configure/////////////////////////////////////////////////
	QMenu* menuConfigure = new QMenu(this);
	menuConfigure->setTitle(tr("&Configure"));
	
	QAction* define_qr_error = new QAction(this);
	define_qr_error->setText(tr("QR error"));
	define_qr_error->setStatusTip(tr("Define the error of QR method"));
	connect(define_qr_error,SIGNAL(triggered()),this,SLOT(setQRError()));
		
	menuConfigure->addAction(define_qr_error);
	
	
	
	/////////////////////////////////////////////////View/////////////////////////////////////////////////
	QMenu* menuView = new QMenu(this);
	menuView->setTitle(tr("&View"));
		
	computatePanel = new ComputatePanel(this);
	
	panel = new QDockWidget(tr("Vibration Modes"),this);
	panel->setFixedWidth(190);
	panel->setWidget(computatePanel);
	panel->close();
	addDockWidget(Qt::RightDockWidgetArea, panel);
	
	view_panel_action = panel->toggleViewAction();
	view_panel_action->setStatusTip(tr("View computate panel."));
	view_panel_action->setEnabled(false);
	
	//view_matrix_action = ;
	
	
	menuView->addAction(view_panel_action);
	
	menubar = new QMenuBar();
	menubar->addMenu(menuFile);
	menubar->addMenu(menuSystem);
	menubar->addMenu(menuConfigure);
	menubar->addMenu(menuView);
	
	setMenuBar(menubar);
	
	setWindowTitle(tr("Movib"));
}

Window::~Window(){
	delete statusbar;
	delete menubar;
	delete panel;
	delete glWidget;
}

void Window::setNumberMasses(){
	bool ok;
	int i = QInputDialog::getInt(this, tr("Mass-spring System"), tr("Number of masses:"), 3, 2, 100, 1, &ok);
	
	if (ok) glWidget->createMassSpring(i, qrError);
	
	view_panel_action->setEnabled(true);
	panel->show();
	computatePanel->setSystem(glWidget->getSystem());
}

void Window::setNumberFloors(){
	bool ok;
	int i = QInputDialog::getInt(this, tr("Building System"), tr("Number of floors:"), 3, 2, 100, 1, &ok);
	
	if (ok) glWidget->createBuilding(i, qrError);
	
	view_panel_action->setEnabled(true);
	panel->show();
	computatePanel->setSystem(glWidget->getSystem());
}

void Window::showGraphic(){
	GraphicWindow *gw = new GraphicWindow();
	gw->show();
}

void Window::setQRError(){
	bool ok = false;
	double i = 0;
	while (i <= 0){
	  i = QInputDialog::getDouble(this, tr("Define QR method`s error"), tr("Error:"), qrError, 1e-09, 10.0, 10, &ok);
	}
	if (ok) 
	  qrError = i;
	else
	  std::cout << "!!" << std::endl;
	std::cout << "i" << i << std::endl;
}
